/**
 * @Title: JVMMonitorService.java
 * @Package: com.itic.appbase.applications.monitor.service
 * @author: idong
 * @date: May 22, 2015 4:10:46 PM
 * @version: V1.0
 */

package com.itic.appbase.applications.monitor.service;

import com.itic.appbase.applications.monitor.vo.JVMInfo;
import com.itic.appbase.applications.monitor.vo.JavaEnvInfo;
import com.itic.appbase.framework.common.service.BaseService;
import com.itic.appbase.framework.utils.ReflectHelper;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

/**
 * <p>
 * Company: itic
 * </p>
 * 
 * @author: idong
 * @date: May 22, 2015 4:10:46 PM
 * @version: V1.0
 */
@Service
public class JVMMonitorService
        extends BaseService {
    // 自定义ENUM,用于赋值操作
    static enum sysProps {

        // java
        VERSION("setVersion", "java.version"), RM_VERSION("setRmVersion", "java.runtime.version"), RM_NAME("setRmName",
                "java.runtime.name"), VM_VERSION(
                "setVmVersion", "java.vm.version"), VM_NAME("setVmName", "java.vm.name"), JAVA_HOME("setJavaHome",
                "java.home"), JAVA_LIB("setJavaLib", "java.library.path"),
        // file
        FILE_ENCODING("setFileEncoding", "file.encoding"), FILE_SEPATATOR("setFileSeparator", "file.separator"),
        // user
        USER_NAME("setUserName", "user.name"), LANGUAGE("setLanguage", "user.language"), USER_HOME("setUserHome",
                "user.home"), COUTRY("setContry", "user.country");

        String method;
        String keyValue;

        sysProps(String method, String keyValue) {
            this.method = method;
            this.keyValue = keyValue;
        }

    }

    /**
     * 获取JAVA,FILE,USER相关信息
     * 
     * @return
     */
    public JavaEnvInfo getJavaEnvInfo() {
        JavaEnvInfo javaEnv = new JavaEnvInfo("---");
        // 获取系统属性
        RuntimeMXBean rmb = ManagementFactory.getRuntimeMXBean();
        List<String> args = rmb.getInputArguments();
        StringBuilder sb = new StringBuilder();
        String split = " | ";
        for (String arg : args) {
            sb.append(arg).append(split);
        }
        javaEnv.setJvmParams(sb.toString().substring(0, sb.toString().length() - split.length()));
        Map<String, String> sp = rmb.getSystemProperties();
        // 利用ENUM循环赋值
        for (sysProps p : sysProps.values()) {
            ReflectHelper.invokeMethod(javaEnv, p.method, new Object[] { sp.get(p.keyValue) });
        }
        // 返回
        return javaEnv;
    }

    /**
     * 获取JVM相关信息
     * 
     * @return
     */
    public JVMInfo getJvmInfo() {
        JVMInfo jvm = new JVMInfo("---");
        // 获取non-heap内存
        MemoryMXBean mmb = ManagementFactory.getMemoryMXBean();
        jvm.setNonheapTotal(mmb.getNonHeapMemoryUsage().getInit() / 1024 / 1024 + "M");
        jvm.setNonheapMax(mmb.getNonHeapMemoryUsage().getMax() / 1024 / 1024 + "M");
        jvm.setNonheapUsed(mmb.getNonHeapMemoryUsage().getUsed() / 1024 / 1024 + "M");
        // 获取HEAP内存
        jvm.setHeapTotal(mmb.getHeapMemoryUsage().getInit() / 1024 / 1024 + "M");
        jvm.setHeapMax(mmb.getHeapMemoryUsage().getMax() / 1024 / 1024 + "M");
        jvm.setHeapUsed(mmb.getHeapMemoryUsage().getUsed() / 1024 / 1024 + "M");
        // 返回
        return jvm;
    }

}
